Completed
Push — master ( d6eb80...a478e7 )
by Muhammad Dyas
15s queued 12s
created

handlers.js ➔ helpCommandHandler   A

Complexity

Conditions 1

Size

Total Lines 24
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 24
rs 10
c 0
b 0
f 0
cc 1
1
import {buildMessageBody, buildNameListSection, buildNameListWinnerSection} from './helpers/components.js';
2
import {createMessage, updateMessage} from './helpers/api.js';
3
import {getRandomWinners} from './helpers/utils.js';
4
import {delayUpdateMessage} from './helpers/task.js';
5
import {buildActionResponse} from './helpers/response.js';
6
7
/**
8
 * @param {object} requestBody - list of names
9
 * @returns {Promise<void>} update message
10
 */
11
export async function updateWinnerCardHandler(requestBody) {
12
  const names = requestBody.names;
13
  const winnerCount = requestBody.count ?? 1;
14
  const winner = getRandomWinners(names, winnerCount);
15
  const messageText = `Congratulations! We have shuffled the list of names and the winner is *${winner.join(',')}*.`;
16
  const message = buildMessageBody(buildNameListWinnerSection(names, winner), messageText);
17
  const request = {
18
    name: requestBody.messageId,
19
    requestBody: message,
20
    updateMask: 'text,cardsV2',
21
  };
22
  await updateMessage(request);
23
}
24
25
/**
26
 * @param {array} names - list of name that will be shuffled
27
 * @param {string} space - google chat space name
28
 * @param {string} thread - chat thread/parent
29
 * @param {integer} count - winner count
30
 * @returns {Promise<void>} will post the message to google API
31
 */
32
export async function createMessageFromNameListHandler(names, space, thread = null, count = 1) {
33
  const cardSection = buildNameListSection(names);
34
  const message = buildMessageBody(cardSection);
35
36
  const request = {
37
    parent: space,
38
    threadKey: thread,
39
    requestBody: message,
40
    messageReplyOption: 'REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD',
41
  };
42
  const apiResponse = await createMessage(request);
43
44
  const messageId = apiResponse.data.name;
45
  const payload = {
46
    messageId,
47
    names: names,
48
    count,
49
  };
50
  await delayUpdateMessage(JSON.stringify(payload));
51
}
52
53
/**
54
 * @param {object} event - request body
55
 * @returns {{actionResponse: {type: string}, text: string}} - new message action response
56
 */
57
export function helpCommandHandler(event) {
58
  const message = `Hi ${event.user.displayName}
59
  Here are the list of available commands:
60
  */random* Opens a dialog where you can input the items/names to be shuffled. By default, it is pre-filled with the list of members in the current space.
61
  */random_members {count}* Quickly get a member of the current space. It's using a simple animation during randomizing
62
  */random_gpt {your_prompt}* Get quick random staff using GPT command 
63
  example 1: */random_gpt number from 1 to 100* it will return a number between 1 to 100 (e.g 12)
64
  example 2: */random_gpt place to visit in Egypt* it will return a random place in Egypt
65
  example 3: */random_gpt joke about programmer* it will return a joke related to programmer
66
  example 4: */random_gpt quote for broken heart* it will return a quote to motivate the broken heart person
67
  */config* Displays the current configuration dialog.
68
  
69
  Discover the power of randomization by mentioning this app. By enclosing your request within double quotes, you can retrieve a random staff from the chat message. Alternatively, without double quotes, the app will utilize your message as a GPT command to generate a staff for you.
70
  example 1(with double quote): *@Randombot "Zubair Manta" "Hasan Star" "Kawasaki Honda"* will shuffle the list given
71
  example 2(without double quote): *@Randombot number between 10 and 20* just like */random_gpt* command, it will return a number between 10 and 20
72
  example 3: *@Randombot number between 10 and 20* just like */random_gpt* command, it will return a number between 10 and 20
73
  `;
74
  return {
75
    thread: event.message.thread,
76
    actionResponse: {
77
      type: 'NEW_MESSAGE',
78
    },
79
    text: message,
80
  };
81
}
82
83
/**
84
 *
85
 * @param {object} event - request body
86
 * @returns {{actionResponse: {type: string}}|{actionResponse: {type: string}}} - dialog action response
87
 */
88
export function configCommandHandler(event) {
89
  const message = {
90
    'sections': [
91
      {
92
        'widgets': [
93
          {
94
            'textParagraph': {
95
              'text': 'Here are the current settings of your space. Please note that you cannot update these settings directly. If you need to make changes, please contact us at [email protected] for assistance.',
96
            },
97
          },
98
          {
99
            'textInput': {
100
              'label': 'Shuffle delay (in seconds)',
101
              'type': 'SINGLE_LINE',
102
              'name': 'fieldName',
103
              'hintText': 'The delay before the items/names shuffled',
104
              'value': '10',
105
            },
106
          },
107
          {
108
            'textInput': {
109
              'label': 'Default shuffle count',
110
              'type': 'SINGLE_LINE',
111
              'name': 'shuffle_count',
112
              'hintText': '',
113
              'value': '1',
114
            },
115
          },
116
        ],
117
      },
118
    ],
119
  };
120
  return buildActionResponse('DIALOG', message);
121
}
122